/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.pt.ui;

import java.util.ArrayList;
import java.util.Collections;

import td.client.place.NameTokens;
import td.domain.Category;
import td.domain.Topic;
import td.domain.util.StatisticalComparison;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Artem Shamsutdinov
 *
 */
public class CategoryListingEntry extends Composite {

	private static CategoryListingEntryUiBinder uiBinder = GWT
			.create(CategoryListingEntryUiBinder.class);

	interface CategoryListingEntryUiBinder extends
			UiBinder<Widget, CategoryListingEntry> {
	}

	public CategoryListingEntry() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	Hyperlink categoryName;
	@UiField
	Label categoryNumReads;
	@UiField
	Label categoryNumTaggings;
	@UiField
	Label categoryNumReferences;
	@UiField
	Label categoryNumThoughts;
	@UiField
	Hyperlink categoryTopic1;
	@UiField
	Hyperlink categoryTopic2;
	@UiField
	Hyperlink categoryTopic3;

	public void setCategory(Category category,
			StatisticalComparison comparisonType) {
		categoryName.setText(category.getName());
		String targetHistoryToken = "!listing;name=" + category.getId();
		categoryName.setTargetHistoryToken(targetHistoryToken);

		categoryNumReads.setText("Number of Reads: " + category.getNumReads());
		categoryNumTaggings.setText("Number of Taggings: "
				+ category.getNumTaggings());
		categoryNumReferences.setText("Number of References: "
				+ category.getNumReferences());
		categoryNumThoughts.setText("Number of Thoughts: "
				+ category.getNumThoughts());

		ArrayList<Topic> topics = new ArrayList<Topic>(
				category.getTopics());
		Topic.currentComparisonType = comparisonType;
		Collections.sort(topics);

		int numTopics = topics.size() > 3 ? 3 : topics.size();

		switch (numTopics) {
		case 3:
			setCategoryThoughtHyperlink(categoryTopic3, topics.get(2));
		case 2:
			setCategoryThoughtHyperlink(categoryTopic2, topics.get(1));
		case 1:
			setCategoryThoughtHyperlink(categoryTopic1, topics.get(0));
		}
	}

	private void setCategoryThoughtHyperlink(Hyperlink categoryThought,
			Topic currrentTopic) {
		categoryThought.setText(currrentTopic.getName());
		categoryThought.setTargetHistoryToken(NameTokens.detail + ";name="
				+ currrentTopic.getId());
	}
}
